<?php

namespace SmartWiki\Models;
use DB;
use Cache;
use Carbon\Carbon;
use SmartWiki\Exceptions\DataNullException;
use SmartWiki\Exceptions\FormatException;
use SmartWiki\Exceptions\ResultException;

/**
 * SmartWiki\Models\Dictionary
 *
 * @mixin \Eloquent
 * @property string $id  ID
 * @property string $name 数据字典名称
 * @property integer $project_id 所属项目
 * @property string $db_host 数据库地址
 * @property boolean $db_user 数据库用户
 * @property boolean $db_passwd 数据库密码
 * @property boolean $db_name 数据库名称
 * @property boolean $structure 数据库结构
 * @property string $create_time
 * @property integer $create_at
 * @property string $modify_time
 * @property integer $modify_at
 * @property string $version 当前时间戳
 * @method static \Illuminate\Database\Query\Builder|Dictionary whereDictionaryId($value)
 * @method static \Illuminate\Database\Query\Builder|Dictionary whereDictionaryName($value)
 * @method static \Illuminate\Database\Query\Builder|Dictionary whereProjectId($value)
 * @method static \Illuminate\Database\Query\Builder|Dictionary whereCreateTime($value)
 * @method static \Illuminate\Database\Query\Builder|Dictionary whereCreateAt($value)
 * @method static \Illuminate\Database\Query\Builder|Dictionary whereModifyTime($value)
 * @method static \Illuminate\Database\Query\Builder|Dictionary whereModifyAt($value)
 * @method static \Illuminate\Database\Query\Builder|Dictionary whereVersion($value)
 */
class Dictionary extends ModelBase
{
    protected $table = 'dictionary';
    protected $primaryKey = 'id';
    protected $dateFormat = 'Y-m-d H:i:s';
    protected $guarded = ['id'];

    public $timestamps = false;

    /**
     * 从缓存中获取指定的数据结构
     * @param $id
     * @param bool $update 是否强制更新缓存
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null|Dictionary|Dictionary[]
     */
    public static function getDictionaryFromCache($id,$update = false)
    {
        $key = 'dictionary.id.'.$id;
        $dictionary = $update or Cache::get($key);

        if(empty($dictionary) or $update){
            $dictionary = Dictionary::find($id);
            $expiresAt = Carbon::now()->addHour(12);

            Cache::put($key,$dictionary, $expiresAt);
        }
        return $dictionary;
    }


    /**
     * 删除
     * @param $id
     * @return bool
     */
    public static function deleteDictionary($id)
    {
        $dictionary = Dictionary::find($id);
        $dictionary->delete();
        return true;
    }


    /**
     * 获取数据结构列表
     * @param int $project_id
     * @return array
     */
    public static function getDictionaries($project_id)
    {
        if(empty($project_id)){
            return [];
        }
        $jsonArray = Dictionary::where('project_id','=',$project_id)
            ->select(['id', 'project_id', 'name', 'db_host', 'db_user', 'db_passwd', 'db_name', 'structure'])
            ->get();
       
        return $jsonArray;
    }

    /**
     * 添加或更新数据结构
     * @return bool
     * @throws \Exception
     */
    public function addOrUpdate()
    {
        if(empty($this->name) || mb_strlen($this->name) < 2 || mb_strlen($this->name) > 50){
            throw new FormatException('数据结构名称必须在2-50字之间',80201);
        }
        if(empty($this->db_host)){
            throw new FormatException('数据库地址不能为空',80202);
        }
        if(empty($this->db_user)){
            throw new FormatException('数据库账户不能为空',80203);
        }
        if(empty($this->db_passwd)){
            throw new FormatException('数据库密码不能为空',80204);
        }
        if(empty($this->db_name)){
            throw new FormatException('数据库名称不能为空',80205);
        }


        DB::beginTransaction();
        try{

            if($this->create_time == null){

            }
            $this->save();

            DB::commit();
            return true;
        }catch (\Exception $ex){
            DB::rollBack();
            throw new ResultException($ex->getMessage(),500);
        }
    }

}
